package com.z.sys.controller;

import com.wf.captcha.utils.CaptchaUtil;
import com.z.sys.entity.LogoInfo;
import com.z.sys.service.ILogoInfoService;
import com.z.sys.service.ISysMenuService;
import com.z.sys.vo.BaseInfoVo;
import com.z.sys.vo.HomeInfoVo;
import com.z.sys.vo.LogoInfoVo;
import com.z.sys.vo.MenuVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class HomeController {

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private ILogoInfoService logoInfoMenuService;

    @GetMapping("/loginPage")
    public String loginPage(HttpServletRequest request){
        return "login";
    }
    @GetMapping("/index")
    public String index(){
        return "index";
    }
    @GetMapping("/403")
    public String error403(){
        return "error/403";
    }
    @GetMapping("/404")
    public String error404(){
        return "error/404";
    }
    @GetMapping("/500")
    public String error500(){
        return "error/500";
    }

    /**
     * 生成验证
     *
     * @param request  请求报文
     * @param response 响应报文
     */
    @GetMapping("generate")
    @ApiOperation(value = "生成验证码接口")
    public void generate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(request, response);
    }
    /**
     * 初始化系统名称、菜单、系统首页
     *
     * @param request  请求报文
     * @param response 响应报文
     */
    @GetMapping("init")
    @ApiOperation(value = "初始化参数")
    @ResponseBody
    public BaseInfoVo init(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<MenuVo> menuVoList = sysMenuService.getCurrentUserMenu();
        LogoInfo logoInfo = logoInfoMenuService.getById(1);
        BaseInfoVo baseInfoVo = new BaseInfoVo();
        baseInfoVo.setHomeInfo(new HomeInfoVo("首页", "system/common/index"));
        baseInfoVo.setLogoInfo(new LogoInfoVo(logoInfo.getTitle(), logoInfo.getHref(), logoInfo.getLogoPath()));
        baseInfoVo.setMenuInfo(menuVoList);
        return baseInfoVo;
    }

    public static void main(String[] args) {
        System.out.println(new BCryptPasswordEncoder().encode("0927"));
    }
}
